/**
 * 违章审核记录 编辑页
 *
 * 作者 Wanghw
 * 日期 2024-08-14 10:38:38
 */
layui.use(['jquery','febs', 'form', 'validate','commonJS','fileUpload'], function () {
    var $ = layui.$,
        febs = layui.febs,
        layer = layui.layer,
        fileUpload = layui.fileUpload,
        form = layui.form,
        $view = $('#peccancyIssuanceRecord-edit'),
        commonJS = layui.commonJS,
        validate = layui.validate;

    form.verify(validate);
    form.render();
    initFormValue();

    var fileUploadDiv = fileUpload.render({
        elem: '#fileUpload',
        url: ctx + 'hazard/upload',
        type: 0,//0-图片，1-视频，2-其他
        maxNum:5,
        defaultData: getFileList(),
        isDetail: true,
        done: function (res) {
        },
    });
    function initFormValue() {
        if (peccancyInformation) {
            form.val("peccancyIssuanceRecord-edit-form", {
                "informationId": peccancyInformation.informationId,
                "peccancyNature": peccancyInformation.peccancyNature,
                "tenantId": peccancyInformation.tenantId
            });
        }
        if(peccancyInformation.peccancyType){
            febs.initDictItemLabel($view.find('#peccancyType'),'SAFETY_PECCANCY_TYPE',peccancyInformation.peccancyType)
        }
        if(peccancyInformation.peccancyRootCause){
            febs.initDictItemLabel($view.find('#peccancyRootCause'),'SAFETY_PECCANCY_ROOT_REASON',peccancyInformation.peccancyRootCause)
        }
        if(peccancyInformation.jobOperation){
            febs.initDictItemLabel($view.find('#jobOperation'),'SAFETY_JOB_OPERATION',peccancyInformation.jobOperation)
        }
        if(peccancyInformation.penaltyScoreFlg && peccancyInformation.penaltyScoreFlg =='1'){
            $view.find('input[name="penaltyScore"]').removeAttr('disabled');
            $view.find('input[name="penaltyScore"]').val(peccancyInformation.penaltyScore);
            $view.find('input[name="penaltyScoreFlg"]').attr('checked','true');
        }
        if(peccancyInformation.learnVideoFlg && peccancyInformation.learnVideoFlg =='1'){
            $view.find('input[name="learnVideoFlg"]').attr('checked','true');
        }

        if( peccancyInformation.penaltyAmountFlg && peccancyInformation.penaltyAmountFlg=='1' && peccancyInformation.penaltyAmountStatus !='1'){
            $view.find('input[name="penaltyAmount"]').removeAttr('disabled');
            $view.find('input[name="penaltyAmount"]').val(peccancyInformation.penaltyAmount);
            $view.find('input[name="penaltyAmountFlg"]').attr('checked','true');
        }

        if(peccancyInformation.auditList){
            auditTableIns = febs.table.init({
                elem: $view.find('table[id="peccancyAuditRecordTable"]'),
                id: 'peccancyAuditRecordTable',
                page:false,
                skin: '',
                limit:Number.MAX_VALUE,
                cols: [
                    [
                        {type: 'numbers',title:'序号'},
                        {field: 'auditRealName', title: '审核人姓名', minWidth: 80},
                        {
                            field: 'auditResult', title: '审核结果', width: 100, align: 'center', templet: function (d) {
                                if (d.auditResult == '1') {
                                    return '<span class="layui-badge febs-tag-green">审核通过</span>';
                                } else {
                                    return '<span class="layui-badge febs-tag-magenta">审核退回</span>';
                                }
                            }
                        },
                        {field: 'auditTime', title: '审核时间', width: 160,align:'center'},
                        {field: 'auditReason', title: '审核理由', minWidth: 120 }
                    ]
                ],
                data:peccancyInformation.auditList
            });
        }

        if(peccancyInformation.issuanceList){
            issuanceTableIns = febs.table.init({
                elem: $view.find('table[id="peccancyIssuanceRecordTable"]'),
                id: 'peccancyIssuanceRecordTable',
                page:false,
                skin: '',
                limit:Number.MAX_VALUE,
                cols: [
                    [
                        {type: 'numbers',title:'序号'},
                        {field: 'auditRealName', title: '签发人姓名', minWidth: 80},
                        {
                            field: 'auditResult', title: '签发结果', width: 100, align: 'center', templet: function (d) {
                                if (d.auditResult == '1') {
                                    return '<span class="layui-badge febs-tag-green">签发通过</span>';
                                } else {
                                    return '<span class="layui-badge febs-tag-magenta">签发退回</span>';
                                }
                            }
                        },
                        {field: 'auditTime', title: '签发时间', width: 160,align:'center'},
                        {field: 'auditReason', title: '签发理由', minWidth: 120 },
                    ]
                ],
                data:peccancyInformation.issuanceList
            });
        }

        form.render('checkbox');
    }

    function getFileList() {
        if (peccancyInformation && peccancyInformation.fileList.length>0) {
            return  peccancyInformation.fileList;
        }else {
            return  [];
        }
    }

    form.on('submit(peccancyIssuanceRecord-edit-form-submit)', function (data) {
        if(data.field.learnVideoFlg == "on"){
            data.field.learnVideoFlg = '1';
        }else{
            data.field.learnVideoFlg = '0';
        }
        if(data.field.penaltyAmountFlg == "on"){
            data.field.penaltyAmountFlg = '1';
            if(commonJS.isEmpty(data.field.penaltyAmount)){
                layer.msg('请选输入扣款金额！', {
                    icon: 5,
                    zIndex: 1989202290,
                    time: 1000
                })
                return false
            }
        }else{
            data.field.penaltyAmountFlg = '0';

        }
        if(data.field.penaltyScoreFlg == "on"){
            data.field.penaltyScoreFlg = '1';
            if(commonJS.isEmpty(data.field.penaltyScore)){
                layer.msg('请选输入扣除分数！', {
                    icon: 5,
                    zIndex: 1989202290,
                    time: 1000
                })
                return false
            }
        }else{
            data.field.penaltyScoreFlg = '0';
        }
        addOrUpdate(data.field);
        return false;
    });

    form.on('checkbox(penaltyAmountFlg)',function (data) {
        if(this.checked){
            $view.find('input[name="penaltyAmount"]').removeAttr('disabled');
        }else {
            $view.find('input[name="penaltyAmount"]').val('');
            $view.find('input[name="penaltyAmount"]').attr('disabled');
        }
    });

    form.on('checkbox(penaltyScoreFlg)',function (data) {
        if(this.checked){
            $view.find('input[name="penaltyScore"]').removeAttr('disabled');
        }else {
            $view.find('input[name="penaltyScore"]').val('');
            $view.find('input[name="penaltyScore"]').attr('disabled');
        }
    });

    function addOrUpdate(data) {
        if(data.status == '6'){
            febs.modal.confirm('签发提示', '是否签发违章信息？', function () {
                febs.postArray(ctx + 'peccancyIssuanceRecord', data, function () {
                    layer.closeAll();
                    febs.alert.success('签发成功');
                    $('#febs-peccancyIssuanceRecord').find('#query').trigger('click');
                });
            });
        }else if(data.status == '5'){
            febs.modal.confirm('退回提示', '是否退回违章信息？', function () {
                febs.postArray(ctx + 'peccancyIssuanceRecord', data, function () {
                    layer.closeAll();
                    febs.alert.success('签发退回成功');
                    $('#febs-peccancyIssuanceRecord').find('#query').trigger('click');
                });
            });
        }else if(data.status == '10'){
            febs.modal.confirm('作废提示', '是否作废违章信息？', function () {
                febs.postArray(ctx + 'peccancyIssuanceRecord', data, function () {
                    layer.closeAll();
                    febs.alert.success('作废成功');
                    $('#febs-peccancyIssuanceRecord').find('#query').trigger('click');
                });
            });
        }


    }
});